#pragma once
#include <iostream>
#include <string>
#include <memory>
#include <sys/select.h>
#include <poll.h>

#include "Socket.h"
#include "Log.h"

using namespace LogMudule;
using namespace sockedmodule;

#define NUM sizeof(fd_set) * 8
using std::cout;
using std::endl;
#define MAX 4096
const int gdefaultfd = -1;

class Pollserve
{
public:
    Pollserve(int port)
        : _port(port), _listen_sockfd(std::make_unique<TcpSocket>()), _isrunning(false)
    {
    }
    void Init()
    {
        _listen_sockfd->BuildTcpSocketMethod(_port);
        for (int i = 0; i < MAX; i++)
        {
            _fds[i].fd = gdefaultfd;
            _fds[i].events = 0;
            _fds[i].revents = 0;
        }
        _fds[0].fd = _listen_sockfd->Fd();
        _fds[0].events |= POLLIN;
    }
    void Start()
    {
        _isrunning = true;
        int timeout = -1;
        while (_isrunning)
        {
            int n = poll(_fds, MAX, timeout);
            switch (n)
            {
            case 0:
                std::cout << "time out" << std::endl;
                break;
            case -1:
                std::cerr << "select" << endl;
                break;
            default:
                cout << "有事件就绪了: timeous" << timeout << endl;
                Dispatcher();
                break;
            }
        }
        _isrunning = false;
    }
    void Recver(int i)
    {
        char buffer[1024];
        int n = recv(_fds[i].fd, buffer, sizeof(buffer) - 1, 0);
        if (n > 0)
        {
            buffer[n] = 0;
            cout << "client#" << buffer << endl;
            std::string message = "echo#";
            message += buffer;
            send(_fds[i].fd, message.c_str(), message.size(), 0);
        }
        else if (n == 0)
        {
            LOG(LogLevel::ERROR) << "客户端退出" << _fds[i].fd;
            close(_fds[i].fd);
            _fds[i].fd = gdefaultfd;
            _fds[i].revents = _fds[i].events = 0;
        }
        else
        {
            LOG(LogLevel::ERROR) << "recv error" << _fds[i].fd;
            close(_fds[i].fd);
            _fds[i].fd = gdefaultfd;
            _fds[i].revents = _fds[i].events = 0;
        }
    }
    void Accepter()
    {
        IntAddr client;
        int newsockfd = _listen_sockfd->Accepter(&client);
        if (newsockfd < 0)
        {
            return;
        }
        else
        {
            cout << "客户端获得了一个新的链接 sockfd:" << newsockfd << "client:" << client.Addr() << endl;
            int pos = gdefaultfd;
            for (int i = 0; i < NUM; i++)
            {
                if (_fds[i].fd == gdefaultfd)
                {
                    pos = i;
                    break;
                }
            }
            if (pos == -1)
            {
                LOG(LogLevel::ERROR) << "服务器已经满了";
                close(newsockfd);
            }
            else
            {
                _fds[pos].fd = newsockfd;
                _fds[pos].events = POLLIN;
            }
        }
    }
    void Dispatcher()
    {
        for (int i = 0; i < NUM; i++)
        {
            if (_fds[i].fd == gdefaultfd)
            {
                continue;
            }
            if (_fds[i].fd == _listen_sockfd->Fd())
            {
                if (_fds[i].revents & POLLIN)
                {
                    Accepter();
                }
            }
            else
            {
                if (_fds[i].revents & POLLIN)
                {
                    Recver(i);
                }
            }
        }
    }
    ~Pollserve()
    {
    }

private:
    uint16_t _port;
    std::unique_ptr<Socket> _listen_sockfd;
    bool _isrunning;
    struct pollfd _fds[MAX];
};